﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sustainalytics.DataLayer.Tests.Utility;
using Sustainalytics.Entities;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;

namespace Sustainalytics.DataLayer.Tests
{
    [TestClass]
    public class MostRecentActivityRepositoryTests
    {
        private IMostRecentActivityRepository MostRecentActivityRepo { get; set; }

        [TestInitialize]
        public void Initialize()
        {
            this.MostRecentActivityRepo = MostRecentActivityRepository.GetMostRecentActivityRepository(ConfigurationManager.ConnectionStrings["ClientPlatform_Test"].ConnectionString);

            MostRecentActivityTestingData.CleanupMostRecentActivityRepo(this.MostRecentActivityRepo);
            MostRecentActivityTestingData.InitializeRepo(this.MostRecentActivityRepo);
        }

        [TestMethod]
        public void GetMostRecentActivity_ItemsAreSorted()
        {
            List<int> accountUniverses = new List<int> { 1, 2, 3, 4, 5 };
            Guid userId = new Guid("fa88a9e9-2f83-11e4-83fb-d89d67630ddc");

            List<MostRecentActivity> mostRecentActivityDb = this.MostRecentActivityRepo.GetMostRecentActivity(accountUniverses, userId, 0, 20);

            IEnumerable<MostRecentActivity> mostRecentActivityMem = MostRecentActivityTestingData.MostRecentActivityList.OrderBy(el => el.LastAccessedDate).Skip(0).Take(10);

            Assert.IsTrue(mostRecentActivityDb.FirstOrDefault().CompanyName == mostRecentActivityMem.FirstOrDefault().CompanyName, "Items are not the same");
            Assert.IsTrue(mostRecentActivityDb.LastOrDefault().CompanyName == mostRecentActivityMem.LastOrDefault().CompanyName, "Items are not the same");
            Assert.IsTrue(mostRecentActivityDb.Select(el => el.Id).SequenceEqual(mostRecentActivityMem.Select(el => el.Id)), "Results are not equal.");
        }

        [TestMethod]
        public void GetMostRecentActivity_PagesDoNotOverlap()
        {
            List<int> accountUniverses = new List<int> { 1, 2, 3, 4, 5 };
            Guid userId = new Guid("fa88a9e9-2f83-11e4-83fb-d89d67630ddc");

            int skip1 = 0;
            int take1 = 10;
            List<MostRecentActivity> page1 = this.MostRecentActivityRepo.GetMostRecentActivity(accountUniverses, userId, skip1, take1);

            int skip2 = 10;
            int take2 = 10;
            List<MostRecentActivity> page2 = this.MostRecentActivityRepo.GetMostRecentActivity(accountUniverses, userId, skip2, take2);

            int skip3 = 20;
            int take3 = 10;
            List<MostRecentActivity> page3 = this.MostRecentActivityRepo.GetMostRecentActivity(accountUniverses, userId, skip3, take3);

            IEnumerable<Guid> dbMostRecentActivity1 = page1.Select(el => el.Id).ToList();
            IEnumerable<Guid> dbMostRecentActivity2 = page2.Select(el => el.Id).ToList();
            IEnumerable<Guid> dbMostRecentActivity3 = page3.Select(el => el.Id).ToList();

            Assert.IsTrue(dbMostRecentActivity1.Intersect(dbMostRecentActivity2).Any() == false, "Page1 has elements in common with Page2");
            Assert.IsTrue(dbMostRecentActivity2.Intersect(dbMostRecentActivity3).Any() == false, "Page2 has elements in common with Page3");
        }
    }
}
